package com.lc.cn.managersys.action.SysMaintain;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.lc.cn.managersys.common.JsonUtil;
import com.lc.cn.managersys.model.CategoryForm;
import com.lc.cn.managersys.service.SysMaintain.CategoryMaintainService;

/**
 * 配件维护
 * @author luo_guang
 *
 */
@Controller("/componentMaintain")
public class ComponentMaintainAction extends DispatchAction {

	@Autowired
	private CategoryMaintainService service = null;
	
	private Logger log = LoggerFactory.getLogger(ComponentMaintainAction.class);
	
	
	/**
	 * 查询目录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Object categoryIdTemp = request.getParameter("categoryId");
		int categoryId = 0;
		CategoryForm category = null;
		String json = "";
		if (categoryIdTemp != null) {
			categoryId = Integer.parseInt((String)categoryIdTemp);
			category = service.getCategory(categoryId);
			if (category != null) {
				json = JsonUtil.bean2Json(category);
			}
		}
		
		PrintWriter out = null;
		try {
			response.setContentType("application/json;charset=UTF-8");
			out = response.getWriter();
			out.print(json);
			out.flush();
		} catch (IOException e) {
			log.info("网络错误");
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
		
		return null;
	}
	
	
	/**
	 * 编辑目录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String categoryId = request.getParameter("categoryId");
		String categoryName = request.getParameter("categoryName");
		String categoryDesc = request.getParameter("categoryDesc");
		
		int id = 0;
		if (categoryId != null) {
			id = Integer.parseInt((String)categoryId);
		}
		
		service.updateCategory(id, categoryName, categoryDesc);
		return null;
	}
	
	/**
	 * 删除当前目录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Object categoryId = request.getParameter("id");
		if (categoryId != null) {
			service.deleteCategory(Integer.parseInt((String)categoryId));
		}
		
		return null;
	}
	
	/**
	 * 展示车辆系列
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward listVechileSeries(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String json = "";
		List<CategoryForm> categorys = service.listVechileSeries();
		if (categorys != null) {
			json = JsonUtil.list2Json(categorys);
		}
		
		PrintWriter out = null;
		try {
			response.setContentType("application/json;charset=UTF-8");
			out = response.getWriter();
			out.print(json);
			out.flush();
		} catch (IOException e) {
			log.info("网络错误");
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
		
		return null;
	}
}
